<?php
namespace app\index\controller;
use app\common\lib;

class User extends Base
{
    public function index()
    {
        if(request()->isAjax()) {
            $id = $this->is_index_login()->id;
            $notice = request()->param('notice');
            $res = Model('Home')->save(['notice'=>$notice],['id'=>$id]);
            if(!$res) {
                return show('200','数据库错误！','0');
            }
            return show('200','更新成功！','1');
        }else {
            $user_sesson = Model('Home')->get_this_data($this->is_index_login()->id);
            $user_msg = [
                'username' => $user_sesson['username'],
                'home' => $user_sesson['type_name'].'栋'.$user_sesson['index_name'].'单元'.$user_sesson['home_name'].'室',
                'tel' => $user_sesson['tel'],
                'status' => $user_sesson['status'],
                'notice' => $user_sesson['notice'],
            ];
            $this->assign([
                'user_msg' => $user_msg
            ]);
            return $this->fetch();
        }
    }

    public function login()
    {
        if(request()->isAjax()){
            $data = input('post.');
            //检测验证码
            if (!captcha_check($data['code'])) {
                return show('200','验证码错误！请刷新验证码后重新验证！','0');
            }
            //检测业主是否注册
            try{
                $is_user = Model('Home')->get(['tel'=>$data['tel']]);
            }catch (\Exception $e) {
                return show('200',$e->getMessage(),'0');
            }
            if(!$is_user) {
                return show('200','该用户不存在','0');
            }
            //检测业主密码
            try{
                $ret = Model('Home')->get(['tel'=>$data['tel'],'password'=>lib\Password::setPassword($data['password'])]);
            }catch (\Exception $e) {
                return show('200',$e->getMessage(),'0');
            }
            if(!$ret) {
                return show('200','密码错误','0');
            }
            //检测业主状态，0 待审核 1 正常 2 异常 3 永久封停
            if($ret['status'] == '3') {
                return show('200','该账号存在异常，已被禁止登录!','0');
            }
            session(config('tools.index_session')['session_profix'],$ret,config('tools.index_session')['session']);
            return show('200','欢迎您 '.$ret['username'],'1');
        }else {
            $_type  = Model('MemberType')->get_status_all_msg('1');   //楼栋
            $_index = Model('MemberIndex')->get_status_all_msg('1');   //单元门
            $_home  = Model('MemberHome')->get_status_all_msg('1');   //房间号
            $this->assign([
                '_type' => $_type,
                '_index' => $_index,
                '_home' => $_home,
            ]);
            return $this->fetch();
        }
    }

    public function register()
    {
        if(request()->isAjax()){
            $data = input('post.');
            //检测两次密码是否一致
            $is_pwd = lib\Password::isPassword($data['password'],$data['repassword']);
            if(!$is_pwd) {
                return show('200','两次密码请保持一致!','0');
            }
            //检测是否该用户已注册
            $_is_has = Model('Home')->get(['type_name'=>$data['type_name'],'index_name'=>$data['index_name'],'home_name'=>$data['home_name']]);
            if($_is_has) {
                return show('200','该业主已经被注册!如有疑问可以具体询问物业或网络管理员','0');
            }
            //检测手机号码是否被注册
            $_is_tel = Model('Home')->get(['tel'=>$data['tel']]);
            if($_is_tel) {
                return show('200','该手机号已被小区业主注册!','0');
            }
            //重置密码
            $data['password'] = lib\Password::setPassword($data['password']);
            //保存数据
            $_home_msg = Model('Home')->allowField(true)->save($data);
            if(!$_home_msg) {
                return show('200','数据库错误!','0');
            }
            return show('200','注册成功！欢迎'.$data['username'].' 您的加入!','1');
        }else {
            return show('200','非法操作','0');
        }
    }

    public function logout()
    {
        if(request()->isAjax()){
            session(null,config('tools.index_session')['session']);
            return show('200','退出成功！','1');
        }else {
            return show('200','非法操作','0');
        }
    }

    public function change_password()
    {
        if (request()->isAjax()) {
            $msg = $this->is_index_login();
            $data = input('post.');
            //检测两次密码是否一致
            $is_pwd = lib\Password::isPassword($data['password'],$data['re_password']);
            if(!$is_pwd) {
                return show('200','两次密码请保持一致!','0');
            }
            //愿密码一致
            $old_pwd = $msg['password'];
            $is_old_pwd = lib\Password::isPassword(lib\Password::setPassword($data['old_password']),$old_pwd);
            if(!$is_old_pwd) {
                return show('200','与原密码不一致!','0');
            }
            $update_pwd = Model('Home')->save(['password'=>lib\Password::setPassword($data['password'])],['id'=>$msg['id']]);
            if(!$update_pwd) {
                return show('200','数据库错误!','0');
            }
            return show('200','更新密码成功！','1');
        }else {
            return $this->fetch();
        }
    }

}
